using Domain;
using ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
using WriteAPI.Command.生产计划;

namespace WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionPlanExtendedController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ProductionPlanExtendedController(IMediator mediator)
        {
            _mediator = mediator;
        }
        
        /// <summary>
        /// 添加生产计划（扩展版本）- 支持前端Addscgd.vue表单
        /// </summary>
        /// <param name="request">添加生产计划请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>生产计划ID</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProductionPlan(AddProductionPlanCommandExtended request, CancellationToken cancellationToken)
        { 
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取生产计划列表 - 支持搜索和分页
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页结果</returns>
        [HttpGet]
        public async Task<APIPaging<ProductionPlanViewModel>> GetProductionPlans([FromQuery] GetProductionPlanExtendedCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取生产计划详情
        /// </summary>
        /// <param name="id">生产计划ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>生产计划详情</returns>
        [HttpGet("{id}")]
        public async Task<APIResult<ProductionPlanDetailViewModel>> GetProductionPlanDetail(int id, CancellationToken cancellationToken)
        {
            var request = new GetProductionPlanDetailCommand { Id = id };
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 更新生产计划
        /// </summary>
        /// <param name="request">更新请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<APIResult<bool>> UpdateProductionPlan(UpdateProductionPlanCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 删除生产计划
        /// </summary>
        /// <param name="id">生产计划ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<APIResult<bool>> DeleteProductionPlan(int id, CancellationToken cancellationToken)
        {
            var request = new DelProductionPlanCommandExtended { Id = id };
            return await _mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 导出生产计划
        /// </summary>
        /// <param name="request">导出请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>导出数据</returns>
        [HttpPost]
        public async Task<APIResult<byte[]>> ExportProductionPlan(ExportProductionPlanCommand request, CancellationToken cancellationToken)
        {
            return await _mediator.Send(request, cancellationToken);
        }
    }
} 